package everyday;

import org.junit.jupiter.api.Test;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
 * 枚举相关操作
 *
 * @author gaojie
 * @date 2022-04-15
 */
public class EnumOperation {

    /**
     * 将枚举转换成 map
     */
    @Test
    void testEnum2Map() throws IllegalAccessException {
        Enum1 enum1 = Enum1.BACK;
        Map<String, Object> map = new HashMap<>();
        map.put("key", enum1.name());
        for (Field declaredField : enum1.getClass().getDeclaredFields()) {
            if (!declaredField.isEnumConstant() && !declaredField.isSynthetic()) {
                declaredField.setAccessible(true);
                map.put(declaredField.getName(), declaredField.get(enum1));
            }
        }
        System.out.printf("得到的Map：%s%n", map);
    }

}
